﻿var candidateContentTypeID = "0x012000C8135A5605D92B4BAF6422F43D086144";

function CanShowStartHRWorkflow()
{
	var selectedItems = SP.ListOperation.Selection.getSelectedItems();

	//check if exactly one item is selected
	if (CountDictionary(selectedItems) == 1)
	{
		if (this.itemId != selectedItems[0]['id'])
		{
			this.itemId = selectedItems[0]['id'];
			GetCandidateContentTypeID(this.itemId);
		}
		else if (this.ContentTypeIDCurrent == null)
		{
			return false;
		}
		// proceed only if content type id match
		else if (this.ContentTypeIDCurrent.indexOf(candidateContentTypeID) == 0)
		{
			if (this.ClosedOn == null)
			{
				return true;
			}
			else if (this.ClosedOn != null)
			{
				return false;
			}
		}

	}
	return false;
}

// This function gets "ContentTypeID" field value of candidate item of given item ID
function GetCandidateContentTypeID(itemId)
{
	var clientContext = SP.ClientContext.get_current();
	var currentList = clientContext.get_web().get_lists().getById(SP.ListOperation.Selection.getSelectedList());
	this.singleItem = currentList.getItemById(itemId);

	clientContext.load(this.singleItem, 'ContentType');
	clientContext.executeQueryAsync(Function.createDelegate(this, this.OnSucceeded_GetCandidateContentTypeID), Function.createDelegate(this, this.OnFailed_GetCandidateContentTypeID));
}

// Handler for success calling of GetCandidateContentTypeID function
function OnSucceeded_GetCandidateContentTypeID()
{
	
	// get content type ID
	this.ContentTypeIDCurrent = this.singleItem.get_contentType().get_id().$V_1;

	RefreshCommandUI();
}

// Handler for failed calling of GetCandidateContentTypeID function
function OnFailed_GetCandidateContentTypeID(sender, args)
{
	alert('Error occurred GetCandidateContentTypeID: ' + args.get_message());
}

function ShowHideGroup(group, img)
{
	if ((group == null) || browseris.mac)
		return;
	if (group.style.display != "none")
	{
		group.style.display = "none";
		img.src = "/_layouts/images/plus.gif";
	}
	else
	{
		group.style.display = "";
		img.src = "/_layouts/images/minus.gif";
	}
}